package com.dingwei.superiordiary.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.ViewPagerAdapter;
import com.dingwei.superiordiary.bean.TabFragmentTitleItem;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.fragment.RankingListFragment;
import com.lihao.baselibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/1/17.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class RankingListActivity extends BaseActivity implements Contract.RankingListView {

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.order_tablayout)
    TabLayout orderTablayout;
    @Bind(R.id.m_viewpager)
    ViewPager mViewpager;
    private List<Fragment> list = new ArrayList<>();

    private List<TabFragmentTitleItem> fragmentTitles = new ArrayList<>();

    private String title = "排行榜";
    public int selectPosition = 0;
    @Override
    protected int getContentViewId() {
        return R.layout.activity_ranking_list;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        if (getIntent().hasExtra("activity_title")){
            title = getIntent().getStringExtra("activity_title");
        }else {
            title = "排行榜";
        }
        if (getIntent().hasExtra("selectPosition")){
            selectPosition = getIntent().getIntExtra("selectPosition",0);
        }else {
            selectPosition = 0;
        }

        if (getIntent().hasExtra("fragmentTitles")){
            fragmentTitles = (List<TabFragmentTitleItem>) getIntent().getSerializableExtra("fragmentTitles");
        }else {
            TabFragmentTitleItem item = new TabFragmentTitleItem("默认排序","","");
            fragmentTitles.add(item);
            item = new TabFragmentTitleItem("点击量排序","","3");
            fragmentTitles.add(item);
            item = new TabFragmentTitleItem("评论排序","","4");
            fragmentTitles.add(item);

        }
        tvTitle.setText(title);


             for (int i = 0; i < fragmentTitles.size(); i++) {
            RankingListFragment fragment = new RankingListFragment();
            Bundle bundle = new Bundle();
            bundle.putString("id", fragmentTitles.get(i).id);
            bundle.putString("order", fragmentTitles.get(i).order);
            fragment.setArguments(bundle);
            list.add(fragment);
        }

        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.setmFragments(list, fragmentTitles);
        mViewpager.setAdapter(adapter);
        mViewpager.setOffscreenPageLimit(fragmentTitles.size());
        orderTablayout.setupWithViewPager(mViewpager);

        mViewpager.setCurrentItem(selectPosition);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
